const query = require('../../db/db')
const moment = require('moment')
const { number } = require('joi')
//发表评论
exports.uploadDiscuss = async (req, res) => {
    try {
        const content = req.body.content
        let sql = 'insert into discuss set ?'
        let obj = {
            email: req.auth.email,
            nickname: req.auth.nickname,
            createtime: moment().format('YYYY-MM-DD HH:mm:ss'),
            content,//发表内容
            files: req.auth.files,
            lock: req.auth.email + moment().format('YYYYMMDDHHmmss'),
            beloneTo: req.body.beloneTo,
        }
        console.log("check", obj);
        const result = await query(sql, obj)
        if (result.affectedRows > 0) {
            return res.send({
                status: 0,
                data: obj,
            })
        }
        res.cc('评论失败')
    } catch (error) {
        res.cc('服务器异常' + error)
    }
}
//回复
exports.uploadReply = async (req, res) => {
    try {
        const content = req.body.content
        let sql = 'insert into reply_discuss set ?'
        let obj = {
            email: req.auth.email,
            replyEmail: req.body.replyEmail,
            nickname: req.auth.nickname,
            replyNickname: req.body.replyNickname,
            createtime: moment().format('YYYY-MM-DD HH:mm:ss'),
            content,//发表内容
            files: req.auth.files,
            replyFiles: req.body.replyFiles,
            lock: req.body.lock,
            beloneTo: req.body.beloneTo,
        }
        const result = await query(sql, obj)
        if (result.affectedRows > 0) {
            return res.send({
                status: 0,
                data: obj,
            })
        }
        res.cc('评论失败')
    } catch (error) {
        res.cc('服务器异常' + error)
    }
}
//从数据库获取评论
exports.getDiscuss = async (req, res) => {
    try {

        const beloneTo = req.query.beloneTo
        let sql = "select * from  discuss where beloneTo=?"
        let replySql = "select * from  reply_discuss where beloneTo=?"
        let result = await query(sql, [beloneTo])
        let replyResult = await query(replySql, [beloneTo])
        console.log("评论数据", result);
        res.send({
            status: 0,
            data: result,//评论
            replyData: replyResult,//回复评论
        })
    } catch (error) {
        console.log(error);
        res.cc(error)
    }
}